<template>
	<view class="page-container">
		<use-navbar ref="navbar" :show="true" :back="true" :bg-color="navbarBgcolor" title="添加新课程">
			
		</use-navbar>
		<view style="height: 25vh;"></view>
		<view class="pos-f wh-full" style="top: 0;opacity: 0.05;">
			<!-- <image src="../../static/images/bg2.jpg" mode="aspectFill" class="wh-full"></image> -->
		</view>
		<view style="position: relative;z-index: 100;">
			<view class="tac title">
				<view class="padding-top">
					<text class="fs-34 fwb">添加一个新的课程</text>
				</view>
				<text class="ft-dark-3">Add a new class...</text>
			</view>
			<view class="padding-lg dflex-c">
				<view class="wpre-85">
					<uni-forms ref="baseForm" :modelValue="baseFormData">
						<uni-forms-item label="">
							<uni-easyinput v-model="baseFormData.title" placeholder="请输入课程名称" />
						</uni-forms-item>
						<view style="height: 15rpx;"></view>
						<uni-forms-item label="">
							<view class="dflex">
								<view style="width: 75%;" class="padding-right-sm">
									<uni-easyinput v-model="baseFormData.number" placeholder="请输入课程次数" />
								</view>
								<text>次</text>
							</view>
						</uni-forms-item>
						<view style="height: 15rpx;"></view>
						<uni-forms-item label="">
							<view class="dflex ">
								<view style="width: 75%;" class="padding-right-sm">
									<uni-easyinput v-model="baseFormData.amount" placeholder="请输入课程金额" />
								</view>
								<text>元</text>
							</view>
						</uni-forms-item>
					</uni-forms>
				</view>
			</view>
			<!-- 统一提交 -->
			<view style="height: 75px;"></view>
			<view class="oper-area pos-f dflex-c w-full ">
				<view class="btn small padding-lr-lg border-radius-lg dflex-c padding-tb-xs" style="min-width: 95px;"
					:class="beSaved" @click="onSubmit">
					<text type="primary" class="dblock">
						保存
					</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	// --组合式 API 模板
	import {
		ref,
		getCurrentInstance,
		computed
	} from 'vue'
	import {
		onLoad,
		onShow,
	} from "@dcloudio/uni-app";
	import {
		storeToRefs
	} from 'pinia';
	import {
		useUserStore
	} from '@/store'
	const {
		ctx,
		proxy
	} = getCurrentInstance();
	const navbarBgcolor = ref('')
	const baseFormData = ref({
		title: '',
		number: '',
		amount: ''
	})
	let UserStore = useUserStore()
	console.log(UserStore.member)
	const title = ref("标题")

	// onLoad 接受 A 页面传递的参数
	onLoad((option) => {
		console.log("B 页面 onLoad:", option); //B 页面 onLoad: {id: '1', name: 'uniapp'}
	});

	onShow(() => {
		console.log("B 页面 onShow");
	});
	const onSubmit = () => {
		if (!baseFormData.value.title) return;
		loadData('P_SaaS_MCDA_CreateNewDA', {
			title: baseFormData.value.title,
			number: baseFormData.value.number,
			amount: baseFormData.value.amount
		}, (res) => {
			console.log(res);
			try {
				let _res_ = res.data1[0];
				proxy.$utils.msg(_res_.tips);
				proxy.$utils.timerout(() => {
					uni.showLoading();
					uni.redirectTo({
						url: `./detail?id=${_res_.id}`
					});
				}, 2150)
			} catch (e) {
				//TODO handle the exception
			}
		})
	}


	const loadData = async (type, mode, callback) => {
		let param, _param, _res, __res;
		switch (type) {
			case 'P_SaaS_MCDA_CreateNewDA':
				param = {
					storedPro: 'P_SaaS_MCDA_CreateNewDA',
					paraNum: 4,
					resultSetNum: 1,
					para1: UserStore.member.member_id,
					para2: mode.title,
					para3: mode.number,
					para4: mode.amount
				}
				_res = await getData(param);
				break;

			default:
		}

		if (typeof callback === 'function') {
			// 数据加载完成回调函数
			callback(_res);
		}
	}
	const getData = (param) => {
		let url = proxy.$config.baseUrl;
		let params = {
			loading: false,
			url: '/webapi/eidData.ashx?oper=LTTG_Multifunction',
			method: 'get',
			data: param
		}
		return new Promise((resolve, reject) => {
			proxy.$api.getData(url, params).then((res) => {
				//console.log(res);
				if (res.status == '1') {
					resolve(res);
				} else {
					reject('获取信息失败' + res.msg)
				}
			})
		})
	}
	const beSaved = computed(() => {
		return !baseFormData.value.title ? 'disabled' : '';
	})
</script>

<style lang="less" scoped>

</style>